package com.futuristik.objects;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.futuristik.arkitek.Character;

public class Spring extends ArkitekObject {

	World world;
	Texture spriteSheet;
	Animation anim;
	TextureRegion idleFrame;
	
	float stateTime;
	private boolean startAnimation;
	float strength = 50;
	
	public final static float width = 1;
	public final static float height = .78f;
	
	public Spring(float x, float y){	
		this(x, y, 0);
	}
	
	public Spring(float x, float y, float angle){
		super(x, y, angle);
		
		//Texture info goes here..
		spriteSheet = new Texture(Gdx.files.internal("data/obj/springSprite.png"));
		
		TextureRegion[] animationFrames = new TextureRegion[7];
		TextureRegion fullSpring = new TextureRegion(spriteSheet, 0, 0, 32, 32);
		TextureRegion threefourths = new TextureRegion(spriteSheet, 32, 0, 32, 32);
		TextureRegion halfSpring = new TextureRegion(spriteSheet, 0, 32, 32, 32);
		TextureRegion littleSpring = new TextureRegion(spriteSheet, 32, 32, 32, 32);
		
		animationFrames[0] = threefourths;
		animationFrames[1] = halfSpring;
		animationFrames[2] = littleSpring;
		animationFrames[3] = halfSpring;
		animationFrames[4] = threefourths;
		animationFrames[5] = fullSpring;
		animationFrames[6] = threefourths;
		
		idleFrame = threefourths;
		
		float animationTime = .03f;
		
		startAnimation = false;
		stateTime = 100;
		
		anim = new Animation(animationTime, animationFrames);
	}
	
	public void create(World w){
		world = w;
						
		BodyDef def = new BodyDef(); //initialize Body definition to begin the creation
		def.position.set(new Vector2(x, y));
		objectBody = w.createBody(def); //creates a body from the definition and adds it to the World's list of bodies
 
		PolygonShape poly = new PolygonShape();		
		poly.setAsBox(width, height, new Vector2(width, height), angle); //creates the polygon as a box of given width and height
		FixtureDef fDef = new FixtureDef();
			fDef.friction = 0;
			fDef.shape = poly;
		objectBody.createFixture(fDef); //this attaches the "fixture" to the body within the World and gives it a density
		poly.dispose(); //removes the temporarily generated shape
	}
	
	public void topContact(Character player){
		Body p = player.getBody();
		Vector2 vel = p.getLinearVelocity();
		Vector2 pos = p.getPosition();
		
		startAnimation = true;
		stateTime = 0;
		
		//This reflects the velocity over the normal vector and turns it around!!
		Vector2 velReflection = ray.cpy().mul(vel.dot(ray)).sub(vel).mul(-1);
		Vector2 refProj = ray.cpy().mul(velReflection.cpy().dot(ray));
		Vector2 newVel = velReflection.cpy().sub(refProj);
		//Vector2 newVel = velReflection;
		
		p.setLinearVelocity(newVel.x, 0);
		//System.out.println(vel+" "+ray+" COMPVel: "+newVel);
		p.applyLinearImpulse(strength*ray.x, strength*ray.y, pos.x, pos.y);
	}
	
	public void render(SpriteBatch batch){
		if(startAnimation){
			stateTime = 0;
			startAnimation = false;
		}
		
		TextureRegion currentFrame = anim.getKeyFrame(stateTime, false);			

		stateTime += Gdx.graphics.getDeltaTime();  
		
		batch.draw(currentFrame, x, y, 1, .78f, 2, 2, 1, 1, MathUtils.radiansToDegrees * angle);      
	}
	
}

